#include <iostream>
#include <stdlib.h>
using namespace std;

namespace ns_vector
{
    class vector
    {
    public:
        vector()
            : _nums(nullptr), _size(0), _capacity(0)
        {
        }
        ~vector()
        {
            delete []_nums;
            _nums=nullptr;
            _size=_capacity=0;
        }
        void SeqCheckCapacity()
        {
            if (_size == _capacity)
            {
                // 扩容
                int newcapacity = _capacity ? _capacity * 2 : 4;
                int *newnums = reinterpret_cast<int *>(realloc(_nums, newcapacity * sizeof(int)));
                if (!newnums)
                {
                    cout << "扩容失败!" << endl;
                    exit(1);
                }
                _nums = newnums;
                _capacity = newcapacity;
            }
        }

        void insert(int pos, int data)
        {
            if (pos < 0 && pos > _size)
                return;
            SeqCheckCapacity();

            // 将pos后的数据以结尾开始挪动
            for (int i = _size; i >= pos; i--)
                _nums[i] = _nums[i - 1];

            // 插入
            _nums[pos] = data;
            ++_size;
        }

        void insertFront(int data)
        {
            insert(0, data);
        }

        void insertAfter(int data)
        {
            insert(_size, data);
        }

        void Print()
        {
            for (int i = 0; i < _size; i++)
            {
                cout << _nums[i] << " ";
            }
            cout << endl;
            cout << _size;
        }

        void erase(int pos)
        {
            if (pos < 0 && pos >= _size)
                return;
            for (int i = pos; i < _size - 1; i++)
                _nums[i] = _nums[i + 1];
            --_size;
        }

    private:
        int *_nums;
        int _size;
        int _capacity;
    };

    void test()
    {
        ns_vector::vector v;
        v.insert(0, 1);
        v.insert(1, 2);
        v.insert(0, 6);
        v.insert(3, 5);
        v.Print();
        v.insertFront(0);
        v.insertAfter(7);
        v.Print();
    }

    void test1()
    {
        vector v;
        v.insert(0, 1);
        v.insert(1, 2);

        v.erase(1);
        v.erase(0);
        v.Print();
    }
}

int main()
{
    ns_vector::test();
    cout<<"---------"<<endl;
    ns_vector::test1();
    return 0;
}